# -*- coding: utf-8 -*-
import requests
import datetime
import time
import pymongo
import json
import random
from mongo_config import *
import re

mongo_client = pymongo.MongoClient(MONGO_URL, 27017)
db = mongo_client[MONGO_DB]
collection_jinse = db['jinse_lives']

def recent(recent_url):
    global max_id
    response = requests.get(recent_url).text
    if response:
        try:
            # 取出所需字段
            dict_datas = json.loads(response)
            lives = dict_datas.get('list')[0].get('lives')
            for live in lives:
                tid = live.get('id')
                if tid > max_id:
                    wait_id.append(tid)
        except:
            pass
    if wait_id:
        max_id = wait_id[0]
    return wait_id

def detail_news(url, header, lid):
    pattern = r'【.*? | (.*?)】'
    pattern_content = r'【.*?】(.*?)。'
    response = requests.get(url, headers = header, timeout = 10).text
    if response:
        try:
            dict_datas = json.loads(response)
            lives = dict_datas.get('list')[0].get('lives')
            for live in lives:
                id = live.get('id')
                if id >= lid:
                    content_all = live.get('content')
                    # 正则取出所需信息
                    new_id = 'https://www.jinse.com/lives/'+str(id) + '.htm'
                    title = re.findall(pattern, str(content_all), re.S)[1]
                    content = re.findall(pattern_content, str(content_all), re.S)[0]
                    sort = live.get('sort')
                    issue_time = live.get('created_at')
                    grade = live.get('grade')
                    if sort == '动态' or sort == '行情':
                        rank = 1
                    else:
                        rank = 0
                    # print(new_id,'------',title,'-----------',content,'-------',sort,'---------',issue_time, '---------',rank)
                    save_to_mongo(new_id, sort, title, content, issue_time, rank)
                else:
                    return None
        except:
            pass


def save_to_mongo(id, sort, title, content, issue_time, rank):
    # 构造字典
    data = {}
    data['_id'] = str(id).replace('\n', '')
    data['sort'] = str(sort).replace('\n', '')
    data['title'] = str(title).replace('\n', '')
    data['content'] = str(content).replace('\n', '')
    data['issue_time'] = str(issue_time).replace('\n', '')
    data['rank'] = str(rank).replace('\n', '')
    data['flag'] = 0
    print(data)
    collection_jinse.insert_one(data)

def main():
    recent_url = 'https://api.jinse.com/v4/live/list?limit=20&reading=false'
    head_user_agent = ['Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko',
                       'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36',
                       'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; rv:11.0) like Gecko)',
                       'Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1',
                       'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3',
                       'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12',
                       'Opera/9.27 (Windows NT 5.2; U; zh-cn)',
                       'Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.0',
                       'Opera/8.0 (Macintosh; PPC Mac OS X; U; en)',
                       'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080219 Firefox/2.0.0.12 Navigator/9.0.0.6',
                       'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0)',
                       'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)',
                       'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)',
                       'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Maxthon/4.0.6.2000 Chrome/26.0.1410.43 Safari/537.1 ',
                       'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E; QQBrowser/7.3.9825.400)',
                       'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0 ',
                       'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.92 Safari/537.1 LBBROWSER',
                       'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; BIDUBrowser 2.x)',
                       'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/3.0 Safari/536.11']
    wait_id = recent(recent_url)
    if wait_id:
        nid = wait_id[0]+1
        lid = wait_id[-1]
        # print(nid,'--------------', lid)
        url = "https://api.jinse.com/v4/live/list?limit=20&reading=false&flag=down&id=" + str(nid)
        # 定义随机头部
        header = {
            "User-Agent": head_user_agent[random.randrange(0, len(head_user_agent))]
        }
        # print(url, header.get('User-Agent'))
        detail_news(url, header, lid)
        wait_id.clear()

if __name__ == '__main__':
    max_id = 0
    wait_id = []
    flag = 0
    max_time = 0
    # 获取当前时间
    now = datetime.datetime.now()
    # 启动时间
    # 启动时间为当前时间 加2秒
    sched_timer = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute,
                                    now.second) + datetime.timedelta(seconds=2)
    while (True):
        # 当前时间
        now = datetime.datetime.now()
        # 毫秒级的时间相等成功率很低 而且存在启动时间秒级与当前时间毫秒级比较的问题
        # 后来换成了以下方式，允许1秒之差
        if sched_timer < now < sched_timer + datetime.timedelta(seconds=1):
            time.sleep(1)
            print(now)
            # 运行程序
            main()
            # 将标签设为 1
            flag = 1
        else:
            # 标签控制 表示主程序已运行，才修改定时任务时间
            if flag == 1:
                # 修改定时任务时间 时间间隔为1分钟
                sched_timer = sched_timer + datetime.timedelta(minutes=5)
                flag = 0
